package com.sinoglobal.sinostore;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.util.LogUtils;
import com.sinoglobal.sinostore.system.Constants;
/**
 * 
* @title: BaseActivity.java 
* @package com.chiliring.sinostore 
* @description: TODO Activity 基类  
* @author LK
* @date 2015-1-8 下午7:14:40
 */
public class BaseActivity extends FragmentActivity {
	protected ViewGroup mainBody;  // 主体显示
	protected boolean isTemplate = true; // 是否使用模板
	public View titleBar;// 标题栏
	protected TextView titleView; // 标题
	protected ImageButton templateButtonLeft; // 模板中左则Button,不使用模板时，不会显示;
	protected ImageButton templateButtonRight; // 模板中右则Button,不使用模板时，不会显示;
	protected TextView titleRightText;// 模板中右侧TextView
	protected RelativeLayout baseButRight;// 模板中右侧按钮布局
	private View reLoadView;// 重新加载的 布局
	protected ImageView titleLine;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.shop_template);
		
		initView();
		
		initData();
	}
	protected <T extends View>  T getViewById(View view, int id) {
		return (T) view.findViewById(id);
	}
	protected <T extends View>  T getViewById(int id) {
		return (T) findViewById(id);
	}
	
	private void initView(){
		mainBody = (ViewGroup) this.findViewById(R.id.shop_view_mainBody);
		reLoadView = this.findViewById(R.id.shop_reLoadView);
		titleBar = this.findViewById(R.id.shop_titleBar);
		titleLine = (ImageView) this.findViewById(R.id.shop_title_line);
		templateButtonLeft = (ImageButton) this.findViewById(R.id.shop_title_but_left);
		templateButtonRight = (ImageButton) this.findViewById(R.id.shop_title_but_right);
		titleRightText = (TextView) this.findViewById(R.id.shop_title_right_text);
		baseButRight = (RelativeLayout)this.findViewById(R.id.shop_base_but_right);
		LogUtils.i("dfasfsd"+R.id.shop_reLoadView);
		reLoadView.setVisibility(View.GONE);
		templateButtonLeft.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		titleView = (TextView) findViewById(R.id.shop_title_text);
		if (!isTemplate) {// 不使用模板就把标题栏去掉
			titleBar.setVisibility(View.GONE);
		}
	}
	
	private void initData(){
		//开启监听网络服务
//		Intent intent = new Intent();
//		intent.setAction("com.sinostore.NetworkStateService");
//		startService(intent);
	}
	
	@Override
	public void setContentView(int layoutResID) {
		if (layoutResID == R.layout.shop_template) {
			super.setContentView(layoutResID);
		} else {
			mainBody.removeAllViews();
			mainBody.addView(this.getLayoutInflater().inflate(layoutResID, null));
		}
	}
	
	@Override
	public void setContentView(View view) {
		mainBody.removeAllViews();
		mainBody.addView(view);
	}

	@Override
	public void setContentView(View view, LayoutParams params) {
		mainBody.removeAllViews(); 
		mainBody.addView(view, params);
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		//停止监听网络服务
//		Intent intent = new Intent();
//		intent.setAction("com.sinostore.NetworkStateService");
//		stopService(intent);
	}
	
	/**
	 * 
	* @author LK
	* @methods toActivity 
	* @description 描述信息 跳转activity方法
	* @date 2015-1-13 下午3:00:10
	* @param toClsActivity
	* @param bundle 参数说明
	 */
	public void toActivity(Class<?> toClsActivity, Bundle bundle) {
		Intent intent = new Intent(this, toClsActivity);
		if (bundle != null) {
			intent.putExtras(bundle);
		}
		startActivity(intent);
		overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
	}
	
	/**
	 * 
	* @author LK 
	* @methods showShortToast 
	* @description 描述信息
	* @date 2015-1-14 下午6:48:02
	* @param message 参数说明
	 */
	public void showShortToast(String message){
		Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
	}
	
//	Intent intent = new Intent();
//	intent.setAction("com.sinoglobal.ningxia.Login");
//	startActivity(intent);
	/**
	 * 
	 * @author lK
	 * @createdate 2015-3-24 上午10:57:09
	 * @Description: (判断用户是否登陆)
	 * @return
	 *
	 */
	public boolean isLogin(){
		if(TextUtils.isEmpty(Constants.userCenterId) || TextUtils.isEmpty(Constants.userId)){
			Intent intent = new Intent();
			intent.setAction(getString(R.string.action));
			startActivity(intent);
			return false;
		}
		return true;
	}
	
	
	
}
